<template>
	<view class="indexs">
		<view class="header header_wi" :style="'padding-top: ' + navH + 'px;'">
			<view class="header_height" :style="'height:' + gaodu + 'px;line-height:' + gaodu + 'px;top:' + gaodutops + 'px;'">
				<view class="index_h_cont3">
					<view class="index_wenben_img" @tap="jumpzuo"><image :src="imgUrl + '/dzx_img33.png'" mode="widthFix"></image></view>
					<view class="index_wenben">{{ type == 0 ? $t('page_pintuans_dizhi_add.Add_address') : $t('page_pintuans_dizhi_add.Modify_address') }}</view>
				</view>
			</view>
			<view class="coloe_title">s</view>
		</view>
		<view class="index_center" :style="'padding-top:' + (navH + 50) + 'px;'">
			<view class="dizhi_add_center">
				<view class="dizhi_add_list">
					<view class="dizhi_add_cont">
						<view class="dizhi_add_cont_t1">
							<span class="bitian">*</span>
							{{ $t('page_pintuans_dizhi_add.addressee') }}
						</view>
						<view class="dizhi_add_cont_r"><input type="text" :placeholder="$t('checkout.lianxirentxt')" :value="username" @input="usernameFun" /></view>
					</view>
					<view class="dizhi_add_cont" v-if="address_setting.is_identitycard == 1">
						<view class="dizhi_add_cont_t1">
							<span class="bitian">*</span>
							{{ $t('page_pintuans_dizhi_add.IDcard') }}
						</view>
						<view class="dizhi_add_cont_r">
							<input type="text" :placeholder="$t('page_pintuans_dizhi_add.placeholder1')" :value="identitycard" @input="identitycardFun" />
						</view>
					</view>
					<view class="dizhi_add_cont" v-if="address_setting.is_clearancecode == 1">
						<view class="dizhi_add_cont_t1">
							<span class="bitian">*</span>
							{{ $t('page_pintuans_dizhi_add.Clearance_code') }}
						</view>
						<view class="dizhi_add_cont_r">
							<input type="text" :placeholder="$t('page_pintuans_dizhi_add.placeholder2')" :value="clearancecode" @input="clearancecodeFun" />
						</view>
					</view>
					<view class="dizhi_add_cont" v-if="address_setting.is_tel_code == 1">
						<view class="dizhi_add_cont_t1">
							<span class="bitian">*</span>
							{{ $t('page_pintuans_dizhi_add.Telephone_area_code') }}
						</view>
						<view class="dizhi_add_cont_r"><input type="text" :placeholder="$t('page_pintuans_dizhi_add.placeholder3')" :value="telcode" @input="telcodeFun" /></view>
					</view>
					<view class="dizhi_add_cont">
						<view class="dizhi_add_cont_t1">
							<span class="bitian">*</span>
							{{ $t('page_pintuans_dizhi_add.Contact_number') }}
						</view>
						<view class="dizhi_add_cont_r">
							<input type="number" :placeholder="$t('page_pintuans_dizhi_add.placeholder4')" :value="userphones" @input="userphonesFun" />
						</view>
					</view>
					<view class="dizhi_add_cont" @tap="guojiatz">
						<view class="dizhi_add_cont_t1">
							<span class="bitian">*</span>
							{{ $t('page_pintuans_dizhi_add.Country_region') }}
						</view>
						<view class="dizhi_add_cont_r">
							<text>{{ guojianame }}</text>
							<image :src="imgUrl + '/dzx_img25.png'"></image>
						</view>
					</view>
					<view class="dizhi_add_cont" v-if="address_setting.is_province == 1">
						<view class="dizhi_add_cont_t1">
							<span class="bitian">*</span>
							{{ $t('page_pintuans_dizhi_add.State_province') }}
						</view>
						<view class="dizhi_add_cont_r">
							<input type="text" :placeholder="$t('page_pintuans_dizhi_add.placeholder5')" :value="usersheng" @input="usershengFun" />
						</view>
					</view>
					<view class="dizhi_add_cont" v-if="address_setting.is_city == 1">
						<view class="dizhi_add_cont_t1">
							<span class="bitian">*</span>
							{{ $t('page_pintuans_dizhi_add.city') }}
						</view>
						<view class="dizhi_add_cont_r">
							<input type="text" :placeholder="$t('page_pintuans_dizhi_add.placeholder6')" :value="userchengshi" @input="userchengshiFun" />
						</view>
					</view>
					<view class="dizhi_add_cont" v-if="address_setting.is_region == 1">
						<view class="dizhi_add_cont_t1">
							<span class="bitian">*</span>
							{{ $t('page_pintuans_dizhi_add.region') }}
						</view>
						<view class="dizhi_add_cont_r">
							<input type="text" :placeholder="$t('page_pintuans_dizhi_add.placeholder12')" :value="district" @input="districtFun" />
						</view>
					</view>
					
					<view class="dizhi_add_cont" v-if="address_setting.is_street == 1">
						<view class="dizhi_add_cont_t1">
							<span class="bitian">*</span>
							{{ $t('page_pintuans_dizhi_add.street') }}
						</view>
						<view class="dizhi_add_cont_r">
							<input type="text" :placeholder="$t('page_pintuans_dizhi_add.placeholder7')" :value="userstree" @input="userstreeFun" />
						</view>
					</view>
					
					<view class="dizhi_add_cont" v-if="address_setting.is_usermark == 1">
						<view class="dizhi_add_cont_t1">
							<span class="bitian">*</span>
							{{ $t('page_pintuans_dizhi_add.usermark') }}
						</view>
						<view class="dizhi_add_cont_r">
							<input type="text" :placeholder="$t('page_pintuans_dizhi_add.placeholderusermark')" :value="usermark" @input="usermarkFun" />
						</view>
					</view>
					
					<view class="dizhi_add_cont" v-if="address_setting.is_door == 1">
						<view class="dizhi_add_cont_t1">
							<span class="bitian">*</span>
							{{ $t('page_pintuans_dizhi_add.House_number') }}
						</view>
						<view class="dizhi_add_cont_r"><input type="text" :placeholder="$t('page_pintuans_dizhi_add.placeholder8')" :value="userdoor" @input="userdoorFun" /></view>
					</view>
					<view class="dizhi_add_conttt" v-if="address_setting.is_detail == 1">
						<view class="dizhi_add_cont_t1">
							<span class="bitian">*</span>
							{{ $t('page_pintuans_dizhi_add.Full_address') }}
						</view>
						<view class="dizhi_add_cont_r">
							<textarea class="textareasty" @input="useraddressFun" :value="useraddress"></textarea> 
						</view>
					</view>
					<view class="dizhi_add_cont" v-if="address_setting.is_code == 1">
						<view class="dizhi_add_cont_t1">
							<span class="bitian">*</span>
							{{ $t('page_pintuans_dizhi_add.post_code') }}
						</view>
						<view class="dizhi_add_cont_r">
							<input type="text" :placeholder="$t('page_pintuans_dizhi_add.placeholder10')" :value="usercode" @input="usercodeFun" />
						</view>
					</view>

					<view class="dizhi_add_cont" v-if="address_setting.is_email == 1">
						<view class="dizhi_add_cont_t1">
							<span class="bitian">*</span>
							{{ $t('page_pintuans_dizhi_add.mailboxNumber') }}
						</view>
						<view class="dizhi_add_cont_r">
							<input type="text" :placeholder="$t('page_pintuans_dizhi_add.placeholder11')" :value="useremils" @input="useremilsFun" />
						</view>
					</view>

					<view class="dizhi_add_cont" v-if="address_setting.is_remark == 1">
						<view class="dizhi_add_cont_t1" style="width: 100px">
							<span class="bitian"></span>
							{{ $t('page_pintuans_dizhi_add.remarks') }}
						</view>
						<view class="dizhi_add_cont_r">
							<text style="color: red">{{ address_setting.remark }}</text>
						</view>
					</view>
				</view>
				<view class="dizhi_add_btn" v-if="type == 0">
					<view class="dizhi_add_btns" @tap="baocun" data-id="0">{{ $t('page_pintuans_dizhi_add.Save_address') }}</view>
				</view>
				<view class="dizhi_add_btn" v-if="type == 1">
					<view class="dizhi_add_btns" @tap="baocun" data-id="1">{{ $t('page_pintuans_dizhi_add.Modify_address') }}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
// pages/shouye/shouye_zkc/shouye_zkc.js
const app = getApp();

let request = require('../../../utils/require');

export default {
	data() {
		return {
			navH: app.globalData.navHeight,
			gaodu: app.globalData.height,
			gaodutops: app.globalData.top,
			imgUrl: app.globalData.imgUrl,
			address_setting: uni.getStorageSync('address_setting'),
			guojiaid: '',
			guojianame: this.$t('page_sharing_create.Please_select_country'),
			username: '',
			userphone: '',
			telcode: '',
			userphones: '',
			usersheng: '',
			userchengshi: '',
			userstree: '',
			usermark:'',
			useraddress: '',
			identitycard: '',
			usercode: '',
			userdoor: '',
			district:'',
			clearancecode: '',
			useremils: '',
			type: 0,

			//0是新增，1是修改
			arrlists: {},

			optionsid: ''
		};
	},
	/**
	 * 生命周期函数--监听页面加载
	 */ onLoad(options) {
		var that = this;
		that.type= options.type
		that.optionsid= options.id
		that.getCountryName();
		if (options.type == 1) {
			that.xiugaiurl();
		}
	},
	onShow() {
		var that = this;

		let pages = getCurrentPages();
		let currPage = pages[pages.length - 1];

		if (currPage.guojiaid) {
			this.guojiaid= currPage.guojiaid,
			this.guojianame= currPage.guojianame
		}
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {},
	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {},
	methods: {
		//收件人
		usernameFun(e) {
			console.log(e, 'e???');
			this.username= e.detail.value
		},
		clearancecodeFun(e) {
			this.clearancecode= e.detail.value
		},
		identitycardFun(e) {
			this.identitycard= e.detail.value
		},
		userphoneFun(e) {
			this.userphone= e.detail.value
		},
		
		userphonesFun(e) {
			this.userphones= e.detail.value
		},

		telcodeFun(e) {
			this.telcode= e.detail.value
		},

		usershengFun(e) {
			console.log(e, 'e?');
			this.usersheng= e.detail.value
		},

		userchengshiFun(e) {
			this.userchengshi= e.detail.value
		},
		
		districtFun(e) {
			this.district = e.detail.value
		},

		userstreeFun(e) {
			this.userstree= e.detail.value
		},
		
		usermarkFun(e) {
			this.usermark= e.detail.value
		},

		useraddressFun(e) {
			this.useraddress= e.detail.value
		},

		usercodeFun(e) {
			this.usercode= e.detail.value
		},

		userdoorFun(e) {
			this.userdoor= e.detail.value
		},

		useremilsFun(e) {
			this.useremils= e.detail.value
		},
		getCountryName() {
			var that = this;
			var data = {
				token: uni.getStorageSync('token')
			};
			request.post(`index.php?s=/api/package/getCountryName&wxapp_id=` + uni.getStorageSync('web_id'), data).then(res => {
				if (res.data.code == 1) {
					var model = res.data.data;
					if(model){
						this.guojiaid= model.country_id,
						this.guojianame= model.country
					}else{
						this.guojiaid= uni.getStorageSync('country')['id']
						this.guojianame= uni.getStorageSync('country')['title']
					}
					
					console.log(that.guojiaid, 87654);
				}
			});
		},
		baocun(e) {
			var that = this;
			var strings = that.guojianame + ',' + that.usersheng + ',' + that.userchengshi;
			console.log(that.usersheng, 'strings655565');

			if (e.currentTarget.dataset.id == 0) {
				//添加
				var data = {
					phone: that.userphones,
					country_id: that.guojiaid,
					telcode: that.telcode,
					identitycard: that.identitycard,
					name: that.username,
					clearancecode: that.clearancecode,
					detail: that.useraddress,
					region: strings,
					district:that.district,
					door: that.userdoor,
					userstree: that.userstree,
					code: that.usercode,
					email: that.useremils,
					token: uni.getStorageSync('token')
				};

				if (that.guojianame == that.$t('page_sharing_create.Please_select_country') || that.guojianame == '') {
					uni.showToast({
						title:that.$t('page_sharing_create.Please_select_country'),
						icon: 'none'
					});
					return;
				}
				if (that.address_setting.is_province == 1) {
					if (that.usersheng == ' ' || that.usersheng == '') {
						uni.showToast({
							title: '省/州必填',
							icon: 'none'
						});
						return;
					}
				}

				if (that.address_setting.is_city == 1) {
					if (that.userchengshi == ' ' || that.userchengshi == '') {
						uni.showToast({
							title: '城市必填',
							icon: 'none'
						});
						return;
					}
				}
		
				if (that.address_setting.is_region == 1) {
					if (that.district == ' ' || that.district == '') {
						uni.showToast({
							title: '区必填',
							icon: 'none'
						});
						return;
					}
				}

				if (data.phone == ' ' || data.phone == '') {
					uni.showToast({
						title: '手机号必填',
						icon: 'none'
					});
					return;
				}
				if (data.name == ' ' || data.name == '') {
					uni.showToast({
						title: '收件人姓名必填',
						icon: 'none'
					});
					return;
				}
				if (that.address_setting.is_clearancecode == 1) {
					if (data.clearancecode == ' ' || data.clearancecode == '') {
						uni.showToast({
							title: '个人通关代码必填',
							icon: 'none'
						});
						return;
					}
				}

				if (that.address_setting.is_street == 1) {
					if (data.userstree == ' ' || data.userstree == 0) {
						uni.showToast({
							title: '街道必填',
							icon: 'none'
						});
						return;
					}
				}
				if (that.address_setting.is_door == 1) {
					if (data.door == ' ' || data.door == '') {
						uni.showToast({
							title: '门牌号必填',
							icon: 'none'
						});
						return;
					}
				}
				if (that.address_setting.is_code == 1) {
					if (data.code == ' ' || data.code == '') {
						uni.showToast({
							title: '邮编必填',
							icon: 'none'
						});
						return;
					}
				}
				if (that.address_setting.is_email == 1) {
					if (data.email == ' ' || data.email == '') {
						uni.showToast({
							title: '邮箱必填',
							icon: 'none'
						});
						return;
					}
				}
				if (that.address_setting.usermark == 1) {
					if (data.usermark == ' ' || data.usermark == 0) {
						uni.showToast({
							title: '唛头必填',
							icon: 'none'
						});
						return;
					}
				}

				request.post(`index.php?s=/api/address/add&wxapp_id=` + uni.getStorageSync('web_id'), data).then(res => {
					if (res.data.code == 1) {
						uni.showToast({
							title: res.data.data
						});
						setTimeout(() => {
							uni.navigateBack({
								delta: 0
							});
						}, 1000);
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						});
					}
				});
			} else {
				//编辑
				var data = {
					phone: that.userphones,
					telcode: that.telcode,
					name: that.username,
					identitycard: that.identitycard,
					clearancecode: that.clearancecode,
					detail: that.useraddress,
					country_id: that.guojiaid,
					region: strings,
					district:that.district,
					door: that.userdoor,
					userstree: that.userstree,
					code: that.usercode,
					email: that.useremils,
					usermark: that.usermark,
					token: uni.getStorageSync('token'),
					address_id: that.optionsid
				};
				if (that.guojianame == that.$t('page_sharing_create.Please_select_country') || that.guojianame == '') {
					uni.showToast({
						title:that.$t('page_sharing_create.Please_select_country'),
						icon: 'none'
					});
					return;
				}
				if (that.address_setting.is_province == 1) {
					if (that.usersheng == ' ' || that.usersheng == '') {
						uni.showToast({
							title: '省/州必填',
							icon: 'none'
						});
						return;
					}
				}
				if (that.address_setting.is_city == 1) {
					if (that.userchengshi == ' ' || that.userchengshi == '') {
						uni.showToast({
							title: '城市必填',
							icon: 'none'
						});
						return;
					}
				}
				
				if (that.address_setting.is_region == 1) {
					if (that.district == ' ' || that.district == '') {
						uni.showToast({
							title: '区必填',
							icon: 'none'
						});
						return;
					}
				}
				if (data.phone == ' ' || data.phone == '') {
					uni.showToast({
						title: '手机号必填',
						icon: 'none'
					});
					return;
				}

				if (data.name == ' ' || data.name == '') {
					uni.showToast({
						title: '收件人姓名必填',
						icon: 'none'
					});
					return;
				}

				if (that.address_setting.is_clearancecode == 1) {
					if (data.clearancecode == ' ' || data.clearancecode == '') {
						uni.showToast({
							title: '个人通关代码必填',
							icon: 'none'
						});
						return;
					}
				}

				if (that.address_setting.is_street == 1) {
					if (data.userstree == ' ' || data.userstree == 0) {
						uni.showToast({
							title: '街道必填',
							icon: 'none'
						});
						return;
					}
				}

				if (that.address_setting.is_door == 1) {
					if (data.door == ' ' || data.door == '') {
						uni.showToast({
							title: '门牌号必填',
							icon: 'none'
						});
						return;
					}
				}

				if (that.address_setting.is_code == 1) {
					if (data.code == ' ' || data.code == '') {
						uni.showToast({
							title: '邮编必填',
							icon: 'none'
						});
						return;
					}
				}

				if (that.address_setting.is_email == 1) {
					if (data.email == ' ' || data.email == '') {
						uni.showToast({
							title: '邮箱必填',
							icon: 'none'
						});
						return;
					}
				}

				request.post(`index.php?s=/api/address/edit&wxapp_id=` + uni.getStorageSync('web_id'), data).then(res => {
					if (res.data.code == 1) {
						uni.showToast({
							title: res.data.data
						});
						setTimeout(() => {
							uni.navigateBack({
								delta: 0
							});
						}, 1000);
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						});
					}
				});
			}
		},

		jumpzuo() {
			uni.navigateBack();
		},

		//选择国家
		guojiatz() {
			uni.navigateTo({
				url: '/pages/indexs/baoguoyg_guojia/baoguoyg_guojia'
			});
		},

		xiugaiurl() {
			var that = this;
			var data = {
				token: uni.getStorageSync('token'),
				address_id: that.optionsid
			};
			request.post(`index.php?s=/api/address/detail&wxapp_id=` + uni.getStorageSync('web_id'), data).then(res => {
				if (res.data.code == 1) {
					var xinxi = res.data.data.detail;
					that.username= xinxi.name
					that.userphones= xinxi.phone
					that.usersheng= xinxi.province
					that.userchengshi= xinxi.city
					that.userstree= xinxi.street
					that.useraddress= xinxi.detail
					that.clearancecode= xinxi.clearancecode
					that.identitycard=xinxi.identitycard
					that.telcode= xinxi.tel_code
					that.district = xinxi.region
					that.usercode= xinxi.code
					that.userdoor= xinxi.door
					that.useremils= xinxi.email
					that.usermark= xinxi.usermark
					that.guojianame= xinxi.country
					console.log(res, ';ressssssss');
				}
			});
		}
	}
};
</script>
<style>
@import './dizhi_add.css';
</style>
